package com.example.backend.service;


import com.example.backend.dao.MqttDao;
import com.example.backend.mapper.po.AirPo;
import com.example.backend.mapper.po.HumidityPo;
import com.example.backend.mapper.po.TemperaturePo;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.datatype.jsr310.JavaTimeModule;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.time.OffsetDateTime;
import java.time.format.DateTimeParseException;

@Service
public class MqttService {

//    private HumidityPoMapper humidityPoMapper;
//    private TemperaturePoMapper temperaturePoMapper;

    private MqttDao mqttDao;
    @Autowired
    @Lazy
    public MqttService(MqttDao mqttDao) {
        this.mqttDao = mqttDao;
    }

    public void saveData(String topic, String message) {
        System.out.println("Received message on topic: " + topic);
        System.out.println("Message content: " + message);
        ObjectMapper objectMapper = new ObjectMapper();
        objectMapper.registerModule(new JavaTimeModule());

        try {
            JsonNode jsonNode = objectMapper.readTree(message);
            System.out.println("Parsed JSON: " + jsonNode);
            float value = Float.parseFloat(jsonNode.get("value").asText());
            String timeStr = jsonNode.get("time").asText(); // 解析时间戳字符串
            //LocalDateTime time = LocalDateTime.parse(timeStr);
            LocalDateTime time = OffsetDateTime.parse(timeStr).toLocalDateTime(); // 使用OffsetDateTime解析
            System.out.println("Parsed value: " + value);
            System.out.println("Parsed time: " + time);

            if(topic.equals("temperature")) {
                TemperaturePo temperaturePo = new TemperaturePo();
                temperaturePo.setTimestamp(time);
                temperaturePo.setValue(value);
                this.mqttDao.insert(temperaturePo);
            }
            else if(topic.equals("humidity")) {
                HumidityPo humidityPo = new HumidityPo();
                humidityPo.setTimestamp(time);
                humidityPo.setValue(value);
                this.mqttDao.insert(humidityPo);
            } else if (topic.equals("air")) {
                AirPo airPo = new AirPo();
                airPo.setTimestamp(time);
                airPo.setValue(value);
                this.mqttDao.insert(airPo);
            }
        } catch (JsonProcessingException e) {
            System.err.println("Failed to parse JSON: " + e.getMessage());
            e.printStackTrace();
        } catch (DateTimeParseException e) {
            System.err.println("Failed to parse date time: " + e.getMessage());
            e.printStackTrace();
        } catch (Exception e) {
            System.err.println("An unexpected error occurred: " + e.getMessage());
            e.printStackTrace();
        }
    }


}
